package com.ks.user.aop;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.ks.core.exception.BizExceptionEnum;
import com.ks.core.exception.GunsException;
import com.ks.core.util.JsonUtil;
import com.ks.core.util.MD5Util;
import com.ks.core.util.ToolUtil;
import com.ks.user.dao.RedisDao;
import com.ks.user.entity.UserBase;
import com.ks.user.service.IUserBaseService;
import netscape.javascript.JSObject;
import org.apache.http.HttpHeaders;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.Map;

/**
 * @author kingdee
 * @date 2020/11/26 14:45
 */
@Aspect
@Component
@Order(1)
public class AuthorAdvice {
    @Resource
    private RedisDao redis;
    @Autowired
    private IUserBaseService userBaseService;

    @Pointcut("@annotation(com.ks.user.aop.Author)")
    private void Author() {
    }

    @Before("Author()")
    public void permissionCHeckFirst(JoinPoint joinPoint)throws Throwable{
        try {
            RequestAttributes ra = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
            HttpServletRequest request = sra.getRequest();
            String authorToken = request.getHeader("AuthorToken");
            String md5 = MD5Util.convertMD5(MD5Util.convertMD5(authorToken));
            String token ="";
            int i = md5.lastIndexOf(":");
            String substring = md5.substring(0, i);
            String[] split = md5.split(":");
            String s = split[3];
            String value = redis.getValue(s);
            if (ToolUtil.isNotEmpty(value)){
                UserBase userBase = JsonUtil.stringToBean(value, new UserBase().getClass());
                if (ToolUtil.isNotEmpty(userBase)){
                    String md51 = MD5Util.getMD5(substring + ":" + userBase.getCSalt());
                    token = substring + ":" +md51;
                    if (authorToken.equals(token)){

                    }else {
                        throw new GunsException(BizExceptionEnum.TOKEN_ERROR);
                    }
                }
            }else {
                throw new GunsException(BizExceptionEnum.TOKEN_ERROR);
            }
        }catch (Exception e){
            throw new GunsException(BizExceptionEnum.TOKEN_ERROR);
        }
    }
}
